package com.smartHire.controller.User.Address;

import com.smartHire.domain.Address;
import com.smartHire.service.UserService;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "AddAddressServlet", value = "/AddAddressServlet")
public class AddAddressServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String open_id= request.getParameter("open_id");
        String name = request.getParameter("name");
        String Address = request.getParameter("address");
        String phoneNumber = request.getParameter("phonenumber");
        String status = request.getParameter("status");
        System.out.println(status);
        String address_id = request.getParameter("address_id");
        WebApplicationContext ctx = null;
        //使用框架中的方法，获取容器对象
        System.out.println("status=" + status);

        ServletContext sc = getServletContext();
        ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
        System.out.println("容器对象的信息========"+ctx);

        try {
            UserService userService = (UserService) ctx.getBean("userService");
            Address address = new Address();
            address.setAddress(Address);

            address.setName(name);
            address.setU_Id(open_id);
            address.setPhone_number(phoneNumber);
            address.setDefault_address(0);
            if(status.equals("0"))
            {
                if(userService.addAddress(address)==1)
                {
                    response.getWriter().write("OK");
                }else
                {
                    response.getWriter().write("NO");
                }

            }else if(status.equals("1"))
            {
                address.setAddress_Id(Integer.parseInt(address_id));
                if(userService.updateAddress(address)==1)
                {
                    response.getWriter().write("OK");
                }else
                {
                    response.getWriter().write("NO");
                }
            }

        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
